/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：zengyy
 * 版本：v1.0
 * 创建日期：2013-06-02 18:31:43
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace SRF.Admin
{
    /// <summary>
    /// 功能 Service
    /// </summary>
    public class PermissionService
    {
        readonly Data.DBHelperBase dbhelper = Data.DBHelperFactory.GetDBHelper();

        /// <summary>
        /// 获取功能
        /// </summary>
        /// <param name="id">功能id</param>
        /// <returns>返回功能</returns>
        public Models.Permission Get(string id)
        {
            return dbhelper.GetQuery("admin.permission.get").SetParameter(new { Id = id }).UniqueResult<Models.Permission>();
        }
        /// <summary>
        /// 获取功能列表
        /// </summary>
        /// <returns>返回功能列表</returns>
        public IList<Models.Permission> List(string parentId = null)
        {
            return dbhelper.GetQuery("admin.permission.list").SetParameter("parentId", parentId).List<Models.Permission>();
        }
        /// <summary>
        /// 保存功能
        /// </summary>
        /// <param name="post">功能</param>
        /// <returns>返回功能Id</returns>
        public string Save(Models.Permission permission)
        {
            if (string.IsNullOrEmpty(permission.Id))
            {
                permission.Id = Guid.NewGuid().ToString();
                dbhelper.GetQuery("admin.permission.insert").SetParameter(permission).Execute();
            }
            else
            {
                dbhelper.GetQuery("admin.permission.update").SetParameter(permission).Execute();
            }

            return permission.Id;
        }
        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="id">功能Id</param>
        public void Delete(string id)
        {
            dbhelper.GetQuery("admin.permission.delete").SetParameter("Id", id).Execute();
        }

        public void SaveAuthorzation(string roleId, string[] actions,string[] dataRuleItems)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            {
                dbhelper.GetQuery("admin.permission.deleteAuthorzation").SetParameter("roleId", roleId).Execute();
                if (actions != null)
                {
                    for (int i = 0; i < actions.Length; i++)
                    {
                        dbhelper.GetQuery("admin.permission.insertAuthorzation").SetParameter(new
                        {
                            roleId = roleId,
                            permissionId = actions[i]
                        }).Execute();
                    }
                }
                dbhelper.GetQuery("admin.datarule.deleteAuthorzation").SetParameter("roleId", roleId).Execute();
                if (dataRuleItems != null)
                {
                    for (int i = 0; i < dataRuleItems.Length; i++)
                    {
                        dbhelper.GetQuery("admin.datarule.insertAuthorzation").SetParameter(new
                        {
                            roleId = roleId,
                            DataRuleItemId = dataRuleItems[i]
                        }).Execute();
                    }
                }

                scope.Complete();
            }
        }

        public IList<string> GetActionsByRole(string roleId)
        {
            return dbhelper.GetQuery("admin.permission.getByRole").SetParameter("roleId", roleId).List<string>();
        }
    }
}